package com.dts.evaluate.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.widget.ListView;

import com.dts.evaluate.R;

/**
 * Created by user on 2016/7/28.
 */
public class TableListView extends ListView {
    private Paint paint;
    private int color;
    private float[] scales;

    public TableListView(Context context) {
        super(context);
        init(context);
    }

    public TableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TableListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        color = ContextCompat.getColor(getContext(),R.color.list_devider_color);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(1);

        setDivider(new ColorDrawable(ContextCompat.getColor(context,R.color.list_devider_color)));
        setDividerHeight(1);
    }

    /**
     * 设置竖线比例
     * @param scales
     */
    public void setScales(float[] scales){
        this.scales = scales;
        invalidate();
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);

        paint.setColor(color);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        //先画 边框
        canvas.drawRect(0,0,width,height,paint);

        if(scales == null || scales.length == 0){
            return;
        }

        //画比例竖线
        for (int i = 0;i<scales.length;i++){
            float scale = scales[i];
            float wid = scale*width;
            canvas.drawLine(wid,0,wid,height,paint);
        }

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //设置heightMeasureSpec为AT_MOST模式
        //heightMeasureSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
